package org.example.mallchat.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.example.mallchat.cache.RoleCacheHandler;
import org.example.mallchat.common.event.UserBlackEvent;
import org.example.mallchat.dao.BlackDao;
import org.example.mallchat.dao.UserDao;
import org.example.mallchat.dao.UserRoleDao;
import org.example.mallchat.domain.entity.Black;
import org.example.mallchat.domain.entity.IpInfo;
import org.example.mallchat.domain.entity.User;
import org.example.mallchat.domain.enums.BlackEnum;
import org.example.mallchat.domain.enums.RoleEnum;
import org.example.mallchat.exception.BusinessException;
import org.example.mallchat.exception.ErrorCode;
import org.example.mallchat.service.BlackService;
import org.example.mallchat.service.RoleService;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

// 拉黑用户接口
@Service
public class BlackServiceImpl implements BlackService {

    @Resource
    private RoleService roleService;

    @Resource
    private BlackDao blackDao;

    @Resource
    private UserDao userDao;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    @Transactional
    public Void blackUser(String uid, Long uid1) {
        // 首先校验是否有权限操作
        Long powerByUid = roleService.findPowerByUid(Long.parseLong(uid));
        if (!RoleEnum.ADMIN.getId().equals(powerByUid)){// 没有权限
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        // 查询该用户的ip信息
        User blackUser = userDao.getById(uid1);
        if (ObjectUtil.isNull(blackUser)){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST);
        }

        // 有权限操作，将uid1加入黑名单
        List<Black> blackList = new ArrayList<>();
        Black blackUid = Black.builder()
                .type(BlackEnum.UID.getId())
                .target(uid1.toString())
                .build();
        blackList.add(blackUid);
        // ip拉入黑名单
        IpInfo ipInfo = blackUser.getIpInfo();
        if (ObjectUtil.isNotNull(ipInfo)){
            String createIp = ipInfo.getCreateIp();
            String updateIp = ipInfo.getUpdateIp();
            Black blackNick = Black.builder()
                    .type(BlackEnum.IP.getId())
                    .target(updateIp)
                    .build();
            blackList.add(blackNick);

            if (!createIp.equals(updateIp)){
                Black blackCreateIp = Black.builder()
                        .type(BlackEnum.IP.getId())
                        .target(createIp)
                        .build();
                blackList.add(blackCreateIp);
            }
        }
        blackDao.saveBatch(blackList);

        // 发布消息 给所有人并且修改黑名单缓存，同时修改拉黑用户的的状态
        applicationEventPublisher.publishEvent(new UserBlackEvent(this,blackUser));
        return null;
    }
}
